/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.hutech09.jsale.bsn.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import org.hutech09.jsale.bsn.ISupplierManager;
import org.hutech09.jsale.bsn.common.DataExistsException;
import org.hutech09.jsale.bsn.common.DataNotExistedException;
import org.hutech09.jsale.bsn.common.IGenericDao;
import org.hutech09.jsale.bsn.exception.ItemNotFoundException;
import org.hutech09.jsale.entity.Supplier;

/**
 *
 * @author Nam
 */
@Stateless
public class SupplierManager implements ISupplierManager {

    @EJB
    private IGenericDao dao;

    @Override
    public void update(Supplier supplier) throws ItemNotFoundException {
        try {
            dao.update(supplier);
        } catch (DataNotExistedException ex) {
            throw new ItemNotFoundException(supplier);
        }
    }

    @Override
    public Supplier create(Supplier supplier) {
        try {
            supplier = dao.create(supplier);
        } catch (DataExistsException ex) {
            Logger.getLogger(SupplierManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return supplier;
    }

    @Override
    public List<Supplier> findPaging(int first, int pageSize, String orderBy, boolean desc, Map<String, String> filter) {
        Map<String, Object> myFilter = new HashMap<String, Object>(filter);
        return dao.findPaging(Supplier.class, first, pageSize, orderBy, desc, myFilter);
    }

    @Override
    public Supplier findByID(long supplierId) {
        return dao.find(Supplier.class, supplierId);
    }

    @Override
    public long count() {
        return dao.count(Supplier.class);
    }

    @Override
    public void delete(long supplierId) throws ItemNotFoundException {
        try {
            dao.delete(Supplier.class, supplierId);
        } catch (DataNotExistedException ex) {
            throw new ItemNotFoundException(new Supplier(supplierId));
        }
    }

    @Override
    public long count(Map<String, String> filter) {
        Map<String, Object> myFilter = new HashMap<String, Object>(filter);
        return dao.count(Supplier.class, myFilter);
    }
}
